<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Model;

class RepairSuggestion extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];




    /**
     * 用户建议
     * @param $param
     * @return int|string
     */
    public static function createSuggestion($param = [])
    {
        $add_data = [
            "uid" => $param["user_id"],  //用户id
            "number" => $param["number"] ?? '',
            "img" => $param["img"] ?? '',
            "suggestion" => $param["suggestion"] ?? '',
            "create_time" => time(), //创建时间
        ];
        return self::create($add_data);
    }







    /**
     * 建议管理列表
     * @param $param
     * @return array
     */
    public static function getSuggestionList($param = [])
    {
        $order = ["a.id"=>"desc"];

        $data['data'] = self::alias("a")
            ->field ("a.*,FROM_UNIXTIME(a.create_time) as create_time,c.nickname,c.phone,
             case when a.status=1 then '启用' 
                when a.status=2 then '禁用' 
                end as status_text 
            ")
//            ->join ('recovery_question_cate b','a.cate=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据用户昵称/联系方式搜索
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::alias("a")
            ->field ("a.*,c.nickname,c.phone")
//            ->join ('recovery_question_cate b','a.cate=b.id','left')
            ->join ('user c','a.uid=c.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("c.nickname",'like', '%'.$param["word"].'%')->whereOr("c.phone",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }





}